package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ：wm
 * @date ：Created in 2022年06月07日
 * @description ：
 * @version: 1.0
 */
@Slf4j
@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 实现任务5进行日期的显示值返回
     * @param year 年
     * @param month 月
     * @return
     */
    @Override
    public List<Map> getOrderDay(Integer year, Integer month) {
        log.info("[Service层 year为{} , month为{}]",year,month);
        //从数据库查询当月的日期数据
        List<OrderSetting> orderSettings = orderSettingDao.getDate(year,month);
        //定义List<Map>集合存放返回值
        List<Map> list = new ArrayList<>();
        //将日期转换成字符串,声明显示格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (OrderSetting orderSetting : orderSettings) {
            //获取从数据库查询到的日期值
            Date orderDate = orderSetting.getOrderDate();
            //取day的值
            String format = simpleDateFormat.format(orderDate);
            Integer day = Integer.parseInt(format.substring(8,10));
            //定义map集合
            Map map = new HashMap();
            //通过map集合进行值的存入
            map.put("date",day);
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            //通过list集合进行循环存入
            list.add(map);
        }
        //返回存的list值
        return list;
    }

    /**
     * 实现任务6 基于日历实现预约设置
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //显示从上一层传过来的值
        log.info("[日期为:{}]",orderSetting.getOrderDate());
        log.info("[可预约人数为:{}]",orderSetting.getNumber());
        //改之前先看下已预约人数,可预约人数要比已预约人数多
        Integer reservations = orderSettingDao.editSelect(orderSetting.getOrderDate());
        if ((reservations==null)){
            orderSettingDao.insertnumber(orderSetting);
        }else {
            if ((orderSetting.getNumber()>=reservations)){
                orderSettingDao.editNumberByDate(orderSetting);
            }else {
                throw new BusinessRuntimeException("可预约人数比已预约人数低不能修改");
            }
        }






    }

    @Override
    public OrderSetting getOrderSettingByDate(Date orderDate) {
        return orderSettingDao.getOrderSettingByDate(orderDate);
    }

    @Override
    public void updateReservationsAddOne(Date orderDate) {
        orderSettingDao.updateReservationsAddOne(orderDate);
    }


}
